<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ include file="../common/up.jsp" %>

<div class="panel">
    <div class="panel-heading">
        <span class="panel-title">${requestScope.society.society_name} 协会举办活动</span>
    </div>
    <div class="panel-body">
        <form action="/studentSociety/activity/addActivity" method="post" onsubmit="return validateForm();">
            活动名：<input type="text" class="form-control" name="name" placeholder="请输入活动名称" required><br>
            活动简介：<input type="text" class="form-control" name="intro" placeholder="请输入活动简介" required><br>
            活动地点：<input type="text" class="form-control" name="place" placeholder="请输入活动地点" required><br>
            活动详情：<textarea class="form-control" name="detail" placeholder="请输入活动详情" required></textarea><br>
            开始时间：<input type="datetime-local" class="form-control" name="start" required/>
            结束时间：<input type="datetime-local" class="form-control" name="end" required/>
            <input type="hidden" name="assId" value="${requestScope.society.societyId}"/>
            <input type="submit" class="btn btn-success" value="添加"/>
        </form>
    </div>

    <script>
        // 根据服务端返回的消息显示提示
        const result = "${requestScope.result}";
        if (result === "1") {
            alert("活动添加成功！");
        } else if (result === "0") {
            alert("活动添加失败，请检查输入！");
        }

        // 表单验证函数
        function validateForm() {
            let nameField = document.querySelector("input[name='name']");
            let introField = document.querySelector("input[name='intro']");
            let startField = document.querySelector("input[name='start']");
            let endField = document.querySelector("input[name='end']");

            // 检查字段是否为空或仅包含空格
            if (isWhitespace(nameField.value) || isWhitespace(introField.value)) {
                alert("活动名称和活动简介不能为空或仅包含空格！");
                return false;
            }

            // 检查结束时间是否早于开始时间
            if (new Date(endField.value) <= new Date(startField.value)) {
                alert("结束时间不能早于开始时间！");
                return false;
            }

            return true;  // 如果通过了所有验证，允许提交
        }

        // 检查字符串是否为空或仅包含空格
        function isWhitespace(str) {
            return !str.trim();  // 去除空格后检查字符串是否为空
        }
    </script>
</div>

<%@ include file="../common/down.jsp" %>
